

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-10-01
 * Time: 15:18
 */

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }
}
public class Solution {
    //注意prev的巧用
    //prev一直作为前驱
    public TreeNode prev=null;
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree==null){
            return null;
        }
        TreeNode head=inOrder(pRootOfTree);
        while(head.left!=null){
            head=head.left;
        }
        return head;
    }
    public TreeNode inOrder(TreeNode pRootOfTree){
        if(pRootOfTree==null){
            return null;
        }
        inOrder(pRootOfTree.left);
        pRootOfTree.left=prev;
        if(prev!=null){
            prev.right=pRootOfTree;
        }
        //链接好之后再对prev赋值
        prev=pRootOfTree;
        inOrder(pRootOfTree.right);
        return pRootOfTree;
    }
}
